import {
  getQualityDetectionProjectList,
  addQualityDetectionProject,
  updateQualityDetectionProject,
  deleteQualityDetectionProject,
} from "@/api/basicData/inspectionProject.js";
export const myMethod = {
  // 切换条数
  handleSizeChange(val) {
    this.pageSize = val;
    this.currentPage = 1;
    this.getTableData();
  },
  // 切换页码
  handleCurrentChange(val) {
    this.currentPage = val;
    this.getTableData();
  },
  // 点击查询操作
  queryChange(val) {
    this.form = Object.assign(this.form, val);
    this.currentPage = 1;
    this.getTableData();
  },
  // 重置查询数据
  resetPage() {
    this.pageSize = 15;
    this.currentPage = 1;
    this.totalPage = 0;
    this.$refs.dataTableRef.tableData = [];
    this.form = {};
  },
  // 获取列表数据
  async getTableData() {
    this.$refs.dataTableRef.tableLoading = true;
    try {
      const { qualityDetectionProjectName, WorkOrderCode } = this.form;
      const params = {
        QualityDetectionProjectCode: WorkOrderCode || "", // 质检项目编码
        QualityDetectionProjectName: qualityDetectionProjectName || "", // 质检项目名称
        rows: this.pageSize,
        page: this.currentPage,
      };
      console.log(params);
      const res = await getQualityDetectionProjectList(params);
      if (res.status === 0) {
        this.$refs.dataTableRef.tableData = res.rows || [];
        this.totalPage = res.total || 0;
      } else {
        this.$refs.dataTableRef.tableData = [];
        this.totalPage = 0;
        this.$message.error(res.msg || "查询失败");
      }
      this.$refs.dataTableRef.tableLoading = false;
    } catch (err) {
      this.$message.error(err.msg || "查询失败");
      this.$refs.dataTableRef.tableLoading = false;
    }
  },
  addChange() {
    this.addTitle = "添加检验项目";
    this.$refs.addDialogComRef.dialogVisible = true;
  },
  // 清空弹框的数据
  closeChange() {
    this.inspectionInfo = {};
    this.$refs.inspectionInfoRef.clearValidate();
  },
  // 确认提交操作
  confirmChange() {
    this.$refs.inspectionInfoRef.validate(async (valid) => {
      if (valid) {
        try {
          this.$refs.addDialogComRef.loading = true;
          const {
            qualityDetectionProjectCode,
            qualityDetectionProjectName,
            remark,
          } = this.inspectionInfo;
          const params = {
            qualityDetectionProjectCode: qualityDetectionProjectCode, // 质检项目编码
            qualityDetectionProjectName: qualityDetectionProjectName, // 质检项目名称
            remark: remark || "", // 备注
          };
          const isAdd = !this.inspectionInfo.qualityDetectionProjectId
          const text = isAdd ? "添加" : "修改"
          if(!isAdd) {
            params.qualityDetectionProjectId = this.inspectionInfo.qualityDetectionProjectId
          }
          const apiName = isAdd ? addQualityDetectionProject : updateQualityDetectionProject
          const result = await apiName(params);
          if (result.ok) {
            this.$message.success(text+ "成功");
            this.$refs.addDialogComRef.dialogVisible = false;
            if(isAdd) this.currentPage = 1
            this.closeChange();
            this.getTableData();
          } else {
            this.$message.error((result.err&&result.err.msg) || (text+ "失败"));
          }
          this.$refs.addDialogComRef.loading = false;
        } catch (err) {
          this.$refs.addDialogComRef.loading = false;
          console.error(text + "数据异常:", err);
        }
      }
    });
  },
  detailChange(val) {
    this.updateChange(val)
  },
  updateChange(val) {
    this.addTitle = "修改检验项目";
    this.inspectionInfo = JSON.parse(JSON.stringify(val))	;
    this.$refs.addDialogComRef.dialogVisible = true;
  },
  async deleteChange(val) {
    // delNum用于切换页码
    const delNum = 1
    const oldCurrentPage = this.currentPage
    if (delNum) {
      const temp = Math.ceil(
        (this.totalPage - delNum ? this.totalPage - delNum : 1) / this.pageSize
      );
      if (this.currentPage > temp) {
        this.currentPage = temp;
      }
    }
    this.$refs.dataTableRef.tableLoading = true;
    try {
      const params = {
        id: val.qualityDetectionProjectId || "" // 质检项目id
      };
      const res = await deleteQualityDetectionProject(params);
      if (res.ok) {
        this.$message.success("删除成功");
        this.getTableData()
      } else {
        this.currentPage = oldCurrentPage
        this.$message.error(res.msg || "删除失败");
      }
      this.$refs.dataTableRef.tableLoading = false;
    } catch (err) {
      this.currentPage = oldCurrentPage
      this.$message.error(err.msg || "删除失败");
      this.$refs.dataTableRef.tableLoading = false;
    }
  },
};
